<template>
  <div class="menu">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-button
          @click="$router.push({ name: 'create-menu' })"
        >添加菜单</el-button>
      </div>
      <!-- 展示菜单 -->
       <el-table
        :data="tableData"
        style="width: 100%">
        <el-table-column
          prop="date"
          label="编号"
          width="180"
          type="index">
        </el-table-column>
        <el-table-column
          prop="name"
          label="菜单名称"
          width="180">
        </el-table-column>
        <el-table-column
          prop="level"
          label="菜单级数">
        </el-table-column>
        <el-table-column
          prop="icon"
          label="前端图标"
          width="180">
        </el-table-column>
        <el-table-column
          prop="orderNum"
          label="排序"
          width="180">
        </el-table-column>
        <el-table-column
          label="操作"
          width="180">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleEdit(scope.row)">编辑</el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>
<script>
import { getAllMenus, deleteMenu } from '@/services/menu'
export default {
  name: 'Menu',
  data () {
    return {
      tableData: []
    }
  },
  created () {
    // 加载所有菜单
    this.loadAllMenus()
  },
  methods: {
    async loadAllMenus () {
      const { data } = await getAllMenus()
      if (data.code === '000000') {
        this.tableData = data.data
      }
    },
    handleEdit (rowData) {
      // 点击编辑按钮跳转编辑页面并将参数传给路由
      this.$router.push({
        name: 'edit-menu',
        params: {
          id: rowData.id
        }
      })
    },
    handleDelete (rowData) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        // 删除菜单项
        const { data } = await deleteMenu(rowData.id)
        if (data.code === '000000') {
          // 删除成功，重新加载菜单
          this.loadAllMenus()
        }
        // console.log(data)
        this.$message.success('删除成功!')
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    }
  }
}
</script>
<style lang="scss">

</style>
